export default {
  initLogic(jsonList, baseTags) {
    let jsonConfig = []
    for (let n = 0; n < jsonList.length; n++) {
      let target = {
        enum: jsonList[n].enum,
        logicalOperation: 'and',
        tagList: []
      }
      let list = jsonList[n].labGrps.labs
      for (let i = 0; i < list.length; i++) {
        if (list[i].labs && list[i].labs.length) {
          // let tag = baseTags.find(item => item.assetCode === list[i].labs[0].labId)
          target.tagList[i] = {
            logicalOperation: list[i].logicTyp,
            tagList: []
          }
          for (let j = 0; j < list[i].labs.length; j++) {
            if (list[i].labs[j].labs && list[i].labs[j].labs.length) {
              target.tagList[i].tagList[j] = {
                logicalOperation: list[i].labs[j].logicTyp,
                tagList: []
              }
              for (let k = 0; k < list[i].labs[j].labs.length; k++) {
                let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labs[k].labId)
                target.tagList[i].tagList[j].tagList.push({
                  tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
                  tagNm: tag.assetNm,
                  valueType: tag.valueType.toString(),
                  valueTypeCode: tag.valueTypeCode,
                  tagCode: list[i].labs[j].labs[k].labId,
                  operationType: list[i].labs[j].labs[k].oprator,
                  operationValue: list[i].labs[j].labs[k].valArray.length
                    ? list[i].labs[j].labs[k].valArray
                    : list[i].labs[j].labs[k].valLft
                    ? [list[i].labs[j].labs[k].valLft, list[i].labs[j].labs[k].valRit]
                    : list[i].labs[j].labs[k].val,
                  enums: tag.enums,
                  labTbl: list[i].labs[j].labs[k].labTbl,
                  labFldMap: list[i].labs[j].labs[k].labFldMap,
                  dictDatabaseType: list[i].labs[j].labs[k].datTyp,
                  isDerLabel: list[i].labs[j].labs[k].isDerLabel || '0'
                })
              }
            } else {
              let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labId)
              target.tagList[i].tagList[j] = {
                tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
                tagNm: tag.assetNm,
                valueType: tag.valueType.toString(),
                valueTypeCode: tag.valueTypeCode,
                tagCode: list[i].labs[j].labId,
                operationType: list[i].labs[j].oprator,
                operationValue: list[i].labs[j].valArray.length
                  ? list[i].labs[j].valArray
                  : list[i].labs[j].valLft
                  ? [list[i].labs[j].valLft, list[i].labs[j].valRit]
                  : list[i].labs[j].val,
                enums: tag.enums,
                labTbl: list[i].labs[j].labTbl,
                labFldMap: list[i].labs[j].labFldMap,
                dictDatabaseType: list[i].labs[j].datTyp,
                isDerLabel: list[i].labs[j].isDerLabel || '0'
              }
            }
          }
        } else {
          let tag = baseTags.find(item => item.assetCode === list[i].labId)
          target.tagList[i] = {
            tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
            tagNm: tag.assetNm,
            valueType: tag.valueType.toString(),
            valueTypeCode: tag.valueTypeCode,
            tagCode: list[i].labId,
            operationType: list[i].oprator,
            operationValue: list[i].valArray.length
              ? list[i].valArray
              : list[i].valLft
              ? [list[i].valLft, list[i].valRit]
              : list[i].val,
            enums: tag.enums,
            labTbl: list[i].labTbl,
            labFldMap: list[i].labFldMap,
            dictDatabaseType: list[i].datTyp,
            isDerLabel: list[i].isDerLabel || '0'
          }
        }
      }
      jsonConfig.push(target)
    }
    return jsonConfig
  },
  initParams(formData) {
    let config = []
    formData.forEach(item => {
      let derivedTag = {
        enum: item.enum,
        labGrps: {}
      }
      let tagList = item.tagList
      if (tagList.length) {
        derivedTag.labGrps = {
          labs: [],
          level: 1,
          logicTyp: item.logicalOperation || 'and'
        }
        for (let i = 0; i < tagList.length; i++) {
          if (tagList[i].tagList) {
            derivedTag.labGrps.labs[i] = {
              labs: [],
              level: 2,
              logicTyp: tagList[i].logicalOperation
            }
            for (let j = 0; j < tagList[i].tagList.length; j++) {
              if (tagList[i].tagList[j].tagList) {
                derivedTag.labGrps.labs[i].labs[j] = {
                  labs: [],
                  level: 3,
                  logicTyp: tagList[i].tagList[j].logicalOperation
                }
                for (let k = 0; k < tagList[i].tagList[j].tagList.length; k++) {
                  var target = tagList[i].tagList[j].tagList[k]
                  let item = {
                    tagNm: target.tagNm,
                    labTbl: target.labTbl,
                    oprator: target.operationType,
                    datTyp: target.dictDatabaseType,
                    logicTyp: 'and',
                    labFldMap: target.labFldMap,
                    dtTyp: target.valueType == '4' ? 'yyyyMMdd' : target.valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
                    labId: target.tagCode,
                    isDerLabel: target.isDerLabel
                  }
                  if (target.valueType == '1') {
                    item.val = ''
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = target.operationValue
                  } else if (
                    target.valueType == '4' ||
                    target.valueType == '5' ||
                    target.valueType == '6' ||
                    target.valueType == '7'
                  ) {
                    if (target.operationType === 'a<=x<b') {
                      item.val = ''
                      item.valLft = target.operationValue[0]
                      item.valRit = target.operationValue[1]
                      item.valArray = []
                    } else {
                      item.val = target.operationValue
                      item.valLft = ''
                      item.valRit = ''
                      item.valArray = []
                    }
                  } else {
                    item.val = target.operationValue
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = []
                  }
                  derivedTag.labGrps.labs[i].labs[j].labs[k] = item
                }
              } else {
                var target2 = tagList[i].tagList[j]
                let item = {
                  tagNm: target2.tagNm,
                  labTbl: target2.labTbl,
                  oprator: target2.operationType,
                  datTyp: target2.dictDatabaseType,
                  logicTyp: 'and',
                  labFldMap: target2.labFldMap,
                  dtTyp: target2.valueType == '4' ? 'yyyyMMdd' : target2.valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
                  labId: target2.tagCode,
                  isDerLabel: target2.isDerLabel
                }
                if (target2.valueType == '1') {
                  item.val = ''
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = target2.operationValue
                } else if (
                  target2.valueType == '4' ||
                  target2.valueType == '5' ||
                  target2.valueType == '6' ||
                  target2.valueType == '7'
                ) {
                  if (target2.operationType === 'a<=x<b') {
                    item.val = ''
                    item.valLft = target2.operationValue[0]
                    item.valRit = target2.operationValue[1]
                    item.valArray = []
                  } else {
                    item.val = target2.operationValue
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = []
                  }
                } else {
                  item.val = target2.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
                derivedTag.labGrps.labs[i].labs[j] = item
              }
            }
          } else {
            let item = {
              tagNm: tagList[i].tagNm,
              labTbl: tagList[i].labTbl,
              oprator: tagList[i].operationType,
              datTyp: tagList[i].dictDatabaseType,
              logicTyp: 'and',
              labFldMap: tagList[i].labFldMap,
              dtTyp: tagList[i].valueType == '4' ? 'yyyyMMdd' : tagList[i].valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
              labId: tagList[i].tagCode,
              isDerLabel: tagList[i].isDerLabel
            }
            if (tagList[i].valueType == '1') {
              item.val = ''
              item.valLft = ''
              item.valRit = ''
              item.valArray = tagList[i].operationValue
            } else if (
              tagList[i].valueType == '4' ||
              tagList[i].valueType == '5' ||
              tagList[i].valueType == '6' ||
              tagList[i].valueType == '7'
            ) {
              if (tagList[i].operationType === 'a<=x<b') {
                item.val = ''
                item.valLft = tagList[i].operationValue[0]
                item.valRit = tagList[i].operationValue[1]
                item.valArray = []
              } else {
                item.val = tagList[i].operationValue
                item.valLft = ''
                item.valRit = ''
                item.valArray = []
              }
            } else {
              item.val = tagList[i].operationValue
              item.valLft = ''
              item.valRit = ''
              item.valArray = []
            }
            derivedTag.labGrps.labs[i] = item
          }
        }
      }
      config.push(derivedTag)
    })
    return config
  }
}
